﻿Public Class MaGraph

    Private _z As ZedGraph.ZedGraphControl
    Private _pane As ZedGraph.GraphPane

    '调试用缓存
    Public x_buf(99) As Double
    Public y_buf(1)() As Double

    Public Sub New(ByRef z As ZedGraph.ZedGraphControl)
        _z = z
        _pane = _z.GraphPane
        _pane.IsFontsScaled = False '禁用自动调整字体大小
        _pane.Title.FontSpec.Size = 12.0F '设置标题字体
        _pane.XAxis.Title.Text = "Time(s)"

        ReDim y_buf(0)(99)
        ReDim y_buf(1)(99)

    End Sub

    ''' <summary>
    ''' 初始化坐标轴
    ''' </summary>
    ''' <param name="ad">ad设备</param>
    ''' <param name="chs">ad通道配置</param>
    ''' <remarks></remarks>
    Public Sub InitAxis(ad As AdDevice, chs() As AdChannel)
        Dim i As Integer = 0
        _pane.XAxis.Scale.Min = ad.axisXData(0)
        _pane.XAxis.Scale.Max = ad.axisXData(ad.axisXData.Length - 1)
        _pane.YAxisList.Clear()
        For Each ch_index As Integer In ad.selectedChs
            _pane.AddYAxis(chs(ch_index).brief)
            _pane.YAxisList(i).Title.Text = chs(ch_index).brief
            _pane.YAxisList(i).Scale.Max = chs(ch_index).max
            _pane.YAxisList(i).Scale.Min = chs(ch_index).min
            'Debug.Print(ad.selectedChs.IndexOf(ch_index))
            i += 1
        Next
        _pane.AxisChange()
        _z.Refresh()

    End Sub

    ''' <summary>
    ''' 刷新曲线
    ''' </summary>
    ''' <param name="ad"></param>
    ''' <param name="chs"></param>
    ''' <remarks></remarks>
    Public Overloads Sub RefreshCurve(ad As AdDevice, chs() As AdChannel)
        _pane.XAxis.Scale.Min = ad.axisXData(0)
        _pane.XAxis.Scale.Max = ad.axisXData(ad.axisXData.Length - 1)
        InitAxis(ad, chs)
        _pane.CurveList.Clear()
        For Each ch_index As Integer In ad.selectedChs
            Dim curve_ct As ZedGraph.LineItem
            Dim ch As AdChannel = chs(ch_index)
            curve_ct = _pane.AddCurve(ch.brief, ad.axisXData, ch.buffer, ch.lineColor, ZedGraph.SymbolType.None)
            curve_ct.YAxisIndex = ad.selectedChs.IndexOf(ch_index)
            curve_ct.Line.Width = ch.lineWidth
        Next
        _pane.AxisChange()
        _z.Refresh()
    End Sub

    Public Overloads Sub RefreshCurve(title As String, x As Double(), y As Double())
        Dim curve_ct As ZedGraph.LineItem
        _pane.CurveList.Clear()
        curve_ct = _pane.AddCurve(title, x, y, Color.Black, ZedGraph.SymbolType.None)
        curve_ct.Line.Width = 2
        _pane.AxisChange()
        _z.Refresh()
    End Sub

    Public Sub ClearCurve()
        _pane.CurveList.Clear()
    End Sub

    Public Sub AddCurve(title As String, x As Double(), y As Double(), c As Color)
        Dim curve_ct As ZedGraph.LineItem
        curve_ct = _pane.AddCurve(title, x, y, c, ZedGraph.SymbolType.None)
        curve_ct.Line.Width = 2
    End Sub

    Public Overloads Sub RefreshCurve()
        _pane.AxisChange()
        _z.Refresh()
    End Sub

    ''' <summary>
    ''' 设置图例的标题
    ''' </summary>
    ''' <param name="str">标题内容</param>
    ''' <remarks></remarks>
    Public Sub SetTitle(str As String)
        _pane.Title.Text = str
    End Sub

    ''' <summary>
    ''' 设置Y轴
    ''' </summary>
    ''' <param name="title">标题</param>
    ''' <param name="max">最大值</param>
    ''' <param name="min">最小值</param>
    Public Sub SetY(title As String, min As Double, max As Double)
        _pane.YAxis.Title.Text = title
        _pane.YAxis.Scale.Max = max
        _pane.YAxis.Scale.Min = min
    End Sub

End Class
